package com.hifar.test.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hifar.common.QueryParamBean;
import com.hifar.plat.baseModal.pojo.BaseOssFiles;
import com.hifar.plat.baseModal.service.IBaseOssFilesService;
import com.hifar.sys.BusiResponse;
import com.hifar.sys.MinioUtil;
import com.hifar.sys.PageResult;
import com.hifar.sys.SystemUtil;
import com.hifar.test.base.pojo.HfEnvConditions;
import com.hifar.test.base.pojo.VO.EnvConditionsQueryParamsVO;
import com.hifar.test.base.pojo.VO.HfEnvConditionsSaveVO;
import com.hifar.test.base.service.IHfEnvConditionsService;
import com.hifar.test.entrust.constant.EntrustConsts;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author zhuWeiWei
 * @description 基础数据-试验条件
 * @date 2025/1/23 10:19
 */
@RestController
@RequestMapping("/api/busi/test/envConditions")
@Api(tags = "基础数据-试验条件")
public class HfEnvConditionsController {
    @Autowired
    private IHfEnvConditionsService hfEnvConditionsService;
    @Autowired
    private IBaseOssFilesService baseOssFilesService;

    @PostMapping("/add")
    public BusiResponse add(@RequestBody @Validated HfEnvConditionsSaveVO hfEnvConditionsSaveVO) {
        HfEnvConditions hfEnvConditions = new HfEnvConditions();
        BeanUtils.copyProperties(hfEnvConditionsSaveVO, hfEnvConditions);
        hfEnvConditionsService.save(hfEnvConditions);
        String id = hfEnvConditions.getId();

        List<BaseOssFiles> testPhotoList = Optional.ofNullable(hfEnvConditionsSaveVO.getTestPhotoList()).orElse(new ArrayList<>());

        for (BaseOssFiles baseOssFiles : testPhotoList) {
            baseOssFiles.setRefId(id);
            baseOssFiles.setRefType(EntrustConsts.WTD_TEST_PHONE);
        }
        UpdateWrapper<BaseOssFiles> updateFileWrapper = new UpdateWrapper<>();
        updateFileWrapper.lambda()
            .set(BaseOssFiles::getRefId, null)
            .eq(BaseOssFiles::getRefId, id)
            .eq(BaseOssFiles::getRefType, EntrustConsts.WTD_TEST_PHONE);
        baseOssFilesService.update(updateFileWrapper);
        baseOssFilesService.updateBatchById(testPhotoList);
        return BusiResponse.OK();
    }

    @PostMapping("/edit")
    public BusiResponse edit(@RequestBody @Validated HfEnvConditionsSaveVO hfEnvConditionsSaveVO) {
        HfEnvConditions hfEnvConditions = new HfEnvConditions();
        BeanUtils.copyProperties(hfEnvConditionsSaveVO, hfEnvConditions);
        hfEnvConditionsService.updateById(hfEnvConditions);
        String id = hfEnvConditions.getId();
        List<BaseOssFiles> testPhotoList = Optional.ofNullable(hfEnvConditionsSaveVO.getTestPhotoList()).orElse(new ArrayList<>());

        for (BaseOssFiles baseOssFiles : testPhotoList) {
            baseOssFiles.setRefId(id);
            baseOssFiles.setRefType(EntrustConsts.WTD_TEST_PHONE);
        }
        UpdateWrapper<BaseOssFiles> updateFileWrapper = new UpdateWrapper<>();
        updateFileWrapper.lambda()
            .set(BaseOssFiles::getRefId, null)
            .eq(BaseOssFiles::getRefId, id)
            .eq(BaseOssFiles::getRefType, EntrustConsts.WTD_TEST_PHONE);
        baseOssFilesService.update(updateFileWrapper);
        baseOssFilesService.updateBatchById(testPhotoList);

        return BusiResponse.OK();
    }

    @PostMapping("/delete")
    public BusiResponse delete(@RequestBody List<String> idList) {
        hfEnvConditionsService.removeByIds(idList);
        return BusiResponse.OK();
    }

    @GetMapping("/queryById")
    public BusiResponse queryById(@RequestParam("id") String id) {
        HfEnvConditions hfEnvConditions = hfEnvConditionsService.getById(id);
        HfEnvConditionsSaveVO hfEnvConditionsSaveVO = new HfEnvConditionsSaveVO();
        BeanUtils.copyProperties(hfEnvConditions, hfEnvConditionsSaveVO);

        List<BaseOssFiles> testPhotoList = MinioUtil.listByRefId(id, EntrustConsts.WTD_TEST_PHONE);
        hfEnvConditionsSaveVO.setTestPhotoList(testPhotoList);

        return BusiResponse.OK(hfEnvConditionsSaveVO);
    }

    @PostMapping("/listPage")
    public BusiResponse listPage(@RequestBody @Validated EnvConditionsQueryParamsVO queryParamsVO) {
        QueryParamBean<HfEnvConditions> queryParamBean = new QueryParamBean<>(queryParamsVO, HfEnvConditions.class);
        QueryWrapper<HfEnvConditions> queryWrapper = queryParamBean.getQueryWrapper();
        Page page = queryParamBean.getPage();
        String unitId = queryParamsVO.getUnitId();
        queryWrapper.lambda()
            .eq(HfEnvConditions::getUnitId, unitId);
        Page<HfEnvConditions> pageData = hfEnvConditionsService.page(page, queryWrapper);
        Page<HfEnvConditionsSaveVO> vpPageData = new Page<>();
        List<HfEnvConditions> records = pageData.getRecords();
        List<HfEnvConditionsSaveVO> recordsVO = new ArrayList<>();
        for (HfEnvConditions record : records) {
            HfEnvConditionsSaveVO hfEnvConditionsSaveVO = new HfEnvConditionsSaveVO();
            BeanUtils.copyProperties(record, hfEnvConditionsSaveVO);
            hfEnvConditionsSaveVO.setTestPhotoList(MinioUtil.listByRefId(record.getId(), EntrustConsts.WTD_TEST_PHONE));
            recordsVO.add(hfEnvConditionsSaveVO);
        }
        BeanUtils.copyProperties(pageData, vpPageData);
        vpPageData.setRecords(recordsVO);
        PageResult pageResult = SystemUtil.parsePage(page, vpPageData);
        return BusiResponse.OK(pageResult);
    }




}
